var sys = require('sys');
var Client = require('mysql').Client;
var client = new Client();

Client.prototype.xxxx = function(){
	return "xxxx";
}

client.user = 'root';
client.password = 'sa';
//client.database = 'db_node';

console.log('Connection to mysql...');

/*
client.connect(function(error, results){
	if(err){
		console.log('Connection error: '+ error.message);
		return;
	}
	
	console.log('Connected to mysql');
	ClientConnectionReady(client);
});
/*
ClientConnectionReady = function(client){
	client.query('USE db_node', function(error, results){
		if(err){
			console.log('ClientConnectionReady Error:'+error.message);
			client.end();
			return;
		}
		//ClientReady(client);
	});
};
*/

client.useDatabase('db_node', function(err,results){
	if(err){
		console.log('Connection error:' + err.message);
		return;
	}
	console.log('Connected to mysql');
	ClientReady(client);
});

ClientReady = function(client){
	var values = ['Chad','Lung','Hello world'];
	client.query('INSERT INTO test SET first_name = ?,last_name = ?, message = ?',values, function(err, results){
		if(err){
			console.log('ClientReady Error:' + err.message);
			client.end();
			return;
		}
		console.log('Inserted' + results.affectedRows+ 'row');
		console.log('Id inserted:' + results.insertId);
	});
	GetData(client);
};

GetData = function(client){
	client.query('SELECT * FROM test', function selectCb(err, results, fields){
		if(err){
			console.log('GetData Error:'+ err.message);
			client.end();
			return;
		}
		
		if(results.length > 0){
			var firstResult = results[0];
			console.log('First Name: '+ firstResult['first_name']);
			console.log('Last Name: '+ firstResult['last_name']);
			console.log('Message: '+ firstResult['message']);
		}
	});
	console.log(client.xxxx());
	client.end();
	console.log('Connection closed');
	
};

